<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Docker Compose 模板文件 | 小鹏杂谈</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="小鹏杂谈">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.8516fda1.css" as="style"><link rel="preload" href="/assets/js/app.b9ccfc55.js" as="script"><link rel="preload" href="/assets/js/4.8390fbcb.js" as="script"><link rel="preload" href="/assets/js/1.3334f995.js" as="script"><link rel="preload" href="/assets/js/34.ba79122c.js" as="script"><link rel="prefetch" href="/assets/js/10.8eb4e356.js"><link rel="prefetch" href="/assets/js/11.31befa4c.js"><link rel="prefetch" href="/assets/js/12.da37eb75.js"><link rel="prefetch" href="/assets/js/13.b24ba73c.js"><link rel="prefetch" href="/assets/js/14.3bbed283.js"><link rel="prefetch" href="/assets/js/15.fac60f8d.js"><link rel="prefetch" href="/assets/js/16.b75d2036.js"><link rel="prefetch" href="/assets/js/17.116317c5.js"><link rel="prefetch" href="/assets/js/18.80fdbeda.js"><link rel="prefetch" href="/assets/js/19.459fc46f.js"><link rel="prefetch" href="/assets/js/20.452f30f9.js"><link rel="prefetch" href="/assets/js/21.99f641dc.js"><link rel="prefetch" href="/assets/js/22.081dc33b.js"><link rel="prefetch" href="/assets/js/23.d3455a59.js"><link rel="prefetch" href="/assets/js/24.8f5d39e0.js"><link rel="prefetch" href="/assets/js/25.c3dc47fb.js"><link rel="prefetch" href="/assets/js/26.d93b5cf2.js"><link rel="prefetch" href="/assets/js/27.6d2089a5.js"><link rel="prefetch" href="/assets/js/28.57336847.js"><link rel="prefetch" href="/assets/js/29.903b5e06.js"><link rel="prefetch" href="/assets/js/3.32100170.js"><link rel="prefetch" href="/assets/js/30.274666a3.js"><link rel="prefetch" href="/assets/js/31.fa9bd0f8.js"><link rel="prefetch" href="/assets/js/32.20179720.js"><link rel="prefetch" href="/assets/js/33.2dcc611d.js"><link rel="prefetch" href="/assets/js/35.bcd9298f.js"><link rel="prefetch" href="/assets/js/36.bf6245d9.js"><link rel="prefetch" href="/assets/js/37.734eb893.js"><link rel="prefetch" href="/assets/js/38.26999070.js"><link rel="prefetch" href="/assets/js/39.cc1f016d.js"><link rel="prefetch" href="/assets/js/40.da84c263.js"><link rel="prefetch" href="/assets/js/41.a96b8efa.js"><link rel="prefetch" href="/assets/js/42.d586cccb.js"><link rel="prefetch" href="/assets/js/43.69bb61aa.js"><link rel="prefetch" href="/assets/js/44.3be5a840.js"><link rel="prefetch" href="/assets/js/45.208a3bc3.js"><link rel="prefetch" href="/assets/js/46.2d6cacad.js"><link rel="prefetch" href="/assets/js/47.5add378a.js"><link rel="prefetch" href="/assets/js/48.587139cc.js"><link rel="prefetch" href="/assets/js/49.57f7c25a.js"><link rel="prefetch" href="/assets/js/5.ff1e84d1.js"><link rel="prefetch" href="/assets/js/50.efde6e4a.js"><link rel="prefetch" href="/assets/js/51.c9447e5f.js"><link rel="prefetch" href="/assets/js/52.57a51c35.js"><link rel="prefetch" href="/assets/js/53.4be0f240.js"><link rel="prefetch" href="/assets/js/54.8dabd349.js"><link rel="prefetch" href="/assets/js/55.3b8ae0e4.js"><link rel="prefetch" href="/assets/js/56.07e830b2.js"><link rel="prefetch" href="/assets/js/57.8c0dbdcc.js"><link rel="prefetch" href="/assets/js/58.63387df6.js"><link rel="prefetch" href="/assets/js/59.959bb8c8.js"><link rel="prefetch" href="/assets/js/6.56a2b5cd.js"><link rel="prefetch" href="/assets/js/60.1041d86f.js"><link rel="prefetch" href="/assets/js/61.f58e6c18.js"><link rel="prefetch" href="/assets/js/62.a1a460c7.js"><link rel="prefetch" href="/assets/js/63.6c611c7d.js"><link rel="prefetch" href="/assets/js/64.6711119c.js"><link rel="prefetch" href="/assets/js/65.4655c3b1.js"><link rel="prefetch" href="/assets/js/66.e5dfe864.js"><link rel="prefetch" href="/assets/js/67.37515de0.js"><link rel="prefetch" href="/assets/js/68.0c8ba92d.js"><link rel="prefetch" href="/assets/js/69.52121d38.js"><link rel="prefetch" href="/assets/js/7.6f143d9f.js"><link rel="prefetch" href="/assets/js/70.1ab4d6a1.js"><link rel="prefetch" href="/assets/js/71.dc99dd1d.js"><link rel="prefetch" href="/assets/js/72.02e21e52.js"><link rel="prefetch" href="/assets/js/73.21efc121.js"><link rel="prefetch" href="/assets/js/74.649f4e1d.js"><link rel="prefetch" href="/assets/js/75.331c13e7.js"><link rel="prefetch" href="/assets/js/76.a94b5ff0.js"><link rel="prefetch" href="/assets/js/8.d4d40f2a.js"><link rel="prefetch" href="/assets/js/9.1b8824e7.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8516fda1.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="小鹏杂谈" class="logo"> <span class="site-name">小鹏杂谈</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    lzpeng723
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>66</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>19</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>Docker Compose 模板文件</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">Docker Compose 模板文件</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>lzpeng723</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>10/3/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>Docker</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="compose-模板文件"><a href="#compose-模板文件" class="header-anchor">#</a> Compose 模板文件</h2> <p>模板文件是使用 <code>Compose</code> 的核心，涉及到的指令关键字也比较多。但大家不用担心，这里面大部分指令跟 <code>docker run</code> 相关参数的含义都是类似的。</p> <p>默认的模板文件名称为 <code>docker-compose.yml</code>，格式为 YAML 格式。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: &quot;3&quot;

services:
  webapp:
    image: examples/web
    ports:
      - &quot;80:80&quot;
    volumes:
      - &quot;/data&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>注意每个服务都必须通过 <code>image</code> 指令指定镜像或 <code>build</code> 指令（需要 Dockerfile）等来自动构建生成镜像。</p> <p>如果使用 <code>build</code> 指令，在 <code>Dockerfile</code> 中设置的选项(例如：<code>CMD</code>, <code>EXPOSE</code>, <code>VOLUME</code>, <code>ENV</code> 等) 将会自动被获取，无需在 <code>docker-compose.yml</code> 中重复设置。</p> <p>下面分别介绍各个指令的用法。</p> <h3 id="build"><a href="#build" class="header-anchor">#</a> <strong><code>build</code></strong></h3> <p>指定 <code>Dockerfile</code> 所在文件夹的路径（可以是绝对路径，或者相对 docker-compose.yml 文件的路径）。 <code>Compose</code> 将会利用它自动构建这个镜像，然后使用这个镜像。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: '3'
services:

  webapp:
    build: ./dir
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>你也可以使用 <code>context</code> 指令指定 <code>Dockerfile</code> 所在文件夹的路径。</p> <p>使用 <code>dockerfile</code> 指令指定 <code>Dockerfile</code> 文件名。</p> <p>使用 <code>arg</code> 指令指定构建镜像时的变量。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: '3'
services:

  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
      args:
        buildno: 1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>使用 <code>cache_from</code> 指定构建镜像的缓存</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>build:
  context: .
  cache_from:
    - alpine:latest
    - corp/web_app:3.14
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="cap-add-cap-drop"><a href="#cap-add-cap-drop" class="header-anchor">#</a> <strong><code>cap_add, cap_drop</code></strong></h3> <p>指定容器的内核能力（capacity）分配。</p> <p>例如，让容器拥有所有能力可以指定为：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>cap_add:
  - ALL
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>去掉 NET_ADMIN 能力可以指定为：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>cap_drop:
  - NET_ADMIN
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="command"><a href="#command" class="header-anchor">#</a> <strong><code>command</code></strong></h3> <p>覆盖容器启动后默认执行的命令。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>command: echo &quot;hello world&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="configs"><a href="#configs" class="header-anchor">#</a> <strong><code>configs</code></strong></h3> <p>仅用于 <code>Swarm mode</code>，详细内容请查看 <a href="./docker-swarm-mode"><code>Swarm mode</code></a> 一节。</p> <h3 id="cgroup-parent"><a href="#cgroup-parent" class="header-anchor">#</a> <strong><code>cgroup_parent</code></strong></h3> <p>指定父 <code>cgroup</code> 组，意味着将继承该组的资源限制。</p> <p>例如，创建了一个 cgroup 组名称为 <code>cgroups_1</code>。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>cgroup_parent: cgroups_1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="container-name"><a href="#container-name" class="header-anchor">#</a> <strong><code>container_name</code></strong></h3> <p>指定容器名称。默认将会使用 <code>项目名称_服务名称_序号</code> 这样的格式。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>container_name: docker-web-container
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><blockquote><p>注意: 指定容器名称后，该服务将无法进行扩展（scale），因为 Docker 不允许多个容器具有相同的名称。</p></blockquote> <h3 id="deploy"><a href="#deploy" class="header-anchor">#</a> <strong><code>deploy</code></strong></h3> <p>仅用于 <code>Swarm mode</code>，详细内容请查看 <a href="https://yeasy.gitbook.io/docker_practice/swarm_mode" target="_blank" rel="noopener noreferrer"><code>Swarm mode</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 一节</p> <h1 id="devices"><a href="#devices" class="header-anchor">#</a> <strong><code>devices</code></strong></h1> <p>指定设备映射关系。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>devices:
  - &quot;/dev/ttyUSB1:/dev/ttyUSB0&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="depends-on"><a href="#depends-on" class="header-anchor">#</a> <strong><code>depends_on</code></strong></h3> <p>解决容器的依赖、启动先后的问题。以下例子中会先启动 <code>redis</code> <code>db</code> 再启动 <code>web</code></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: '3'

services:
  web:
    build: .
    depends_on:
      - db
      - redis

  redis:
    image: redis

  db:
    image: postgres
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><blockquote><p>注意：<code>web</code> 服务不会等待 <code>redis</code> <code>db</code> 「完全启动」之后才启动。</p></blockquote> <h3 id="dns"><a href="#dns" class="header-anchor">#</a> <strong><code>dns</code></strong></h3> <p>自定义 <code>DNS</code> 服务器。可以是一个值，也可以是一个列表。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>dns: 8.8.8.8

dns:
  - 8.8.8.8
  - 114.114.114.114
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h1 id="dns-search"><a href="#dns-search" class="header-anchor">#</a> <strong><code>dns_search</code></strong></h1> <p>配置 <code>DNS</code> 搜索域。可以是一个值，也可以是一个列表。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>dns_search: example.com

dns_search:
  - domain1.example.com
  - domain2.example.com
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="tmpfs"><a href="#tmpfs" class="header-anchor">#</a> <strong><code>tmpfs</code></strong></h3> <p>挂载一个 tmpfs 文件系统到容器。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>tmpfs: /run
tmpfs:
  - /run
  - /tmp
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="env-file"><a href="#env-file" class="header-anchor">#</a> <strong><code>env_file</code></strong></h3> <p>从文件中获取环境变量，可以为单独的文件路径或列表。</p> <p>如果通过 <code>docker-compose -f FILE</code> 方式来指定 Compose 模板文件，则 <code>env_file</code> 中变量的路径会基于模板文件路径。</p> <p>如果有变量名称与 <code>environment</code> 指令冲突，则按照惯例，以后者为准。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>env_file: .env

env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>环境变量文件中每一行必须符合格式，支持 <code>#</code> 开头的注释行。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code># common.env: Set development environment
PROG_ENV=development
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="environment"><a href="#environment" class="header-anchor">#</a> <strong><code>environment</code></strong></h3> <p>设置环境变量。你可以使用数组或字典两种格式。</p> <p>只给定名称的变量会自动获取运行 Compose 主机上对应变量的值，可以用来防止泄露不必要的数据。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>environment:
  RACK_ENV: development
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SESSION_SECRET
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>如果变量名称或者值中用到 <code>true|false，yes|no</code> 等表达 <a href="https://yaml.org/type/bool.html" target="_blank" rel="noopener noreferrer">布尔<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 含义的词汇，最好放到引号里，避免 YAML 自动解析某些内容为对应的布尔语义。这些特定词汇，包括</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="expose"><a href="#expose" class="header-anchor">#</a> <strong><code>expose</code></strong></h3> <p>暴露端口，但不映射到宿主机，只被连接的服务访问。</p> <p>仅可以指定内部端口为参数</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>expose:
 - &quot;3000&quot;
 - &quot;8000&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="external-links"><a href="#external-links" class="header-anchor">#</a> <strong><code>external_links</code></strong></h3> <blockquote><p>注意：不建议使用该指令。</p></blockquote> <p>链接到 <code>docker-compose.yml</code> 外部的容器，甚至并非 <code>Compose</code> 管理的外部容器。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_1:postgresql
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="extra-hosts"><a href="#extra-hosts" class="header-anchor">#</a> <strong><code>extra_hosts</code></strong></h3> <p>类似 Docker 中的 <code>--add-host</code> 参数，指定额外的 host 名称映射信息。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>extra_hosts:
 - &quot;googledns:8.8.8.8&quot;
 - &quot;dockerhub:52.1.157.61&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>会在启动后的服务容器中 <code>/etc/hosts</code> 文件中添加如下两条条目。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>8.8.8.8 googledns
52.1.157.61 dockerhub
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="healthcheck"><a href="#healthcheck" class="header-anchor">#</a> <strong><code>healthcheck</code></strong></h3> <p>通过命令检查容器是否健康运行。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>healthcheck:
  test: [&quot;CMD&quot;, &quot;curl&quot;, &quot;-f&quot;, &quot;http://localhost&quot;]
  interval: 1m30s
  timeout: 10s
  retries: 3
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="image"><a href="#image" class="header-anchor">#</a> <strong><code>image</code></strong></h3> <p>指定为镜像名称或镜像 ID。如果镜像在本地不存在，<code>Compose</code> 将会尝试拉取这个镜像。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="labels"><a href="#labels" class="header-anchor">#</a> <strong><code>labels</code></strong></h3> <p>为容器添加 Docker 元数据（metadata）信息。例如可以为容器添加辅助说明信息。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>labels:
  com.startupteam.description: &quot;webapp for a startup team&quot;
  com.startupteam.department: &quot;devops department&quot;
  com.startupteam.release: &quot;rc3 for v1.0&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="links"><a href="#links" class="header-anchor">#</a> <strong><code>links</code></strong></h3> <blockquote><p>注意：不推荐使用该指令。</p></blockquote> <h3 id="logging"><a href="#logging" class="header-anchor">#</a> <strong><code>logging</code></strong></h3> <p>配置日志选项。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>logging:
  driver: syslog
  options:
    syslog-address: &quot;tcp://192.168.0.42:123&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>目前支持三种日志驱动类型。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>driver: &quot;json-file&quot;
driver: &quot;syslog&quot;
driver: &quot;none&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>options</code> 配置日志驱动的相关参数。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>options:
  max-size: &quot;200k&quot;
  max-file: &quot;10&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="network-mode"><a href="#network-mode" class="header-anchor">#</a> <strong><code>network_mode</code></strong></h3> <p>设置网络模式。使用和 <code>docker run</code> 的 <code>--network</code> 参数一样的值。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>network_mode: &quot;bridge&quot;
network_mode: &quot;host&quot;
network_mode: &quot;none&quot;
network_mode: &quot;service:[service name]&quot;
network_mode: &quot;container:[container name/id]&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="networks"><a href="#networks" class="header-anchor">#</a> <strong><code>networks</code></strong></h3> <p>配置容器连接的网络。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: &quot;3&quot;
services:

  some-service:
    networks:
     - some-network
     - other-network

networks:
  some-network:
  other-network:
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="pid"><a href="#pid" class="header-anchor">#</a> <strong><code>pid</code></strong></h3> <p>跟主机系统共享进程命名空间。打开该选项的容器之间，以及容器和宿主机系统之间可以通过进程 ID 来相互访问和操作。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>pid: &quot;host&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="ports"><a href="#ports" class="header-anchor">#</a> <strong><code>ports</code></strong></h3> <p>暴露端口信息。</p> <p>使用宿主端口：容器端口 <code>(HOST:CONTAINER)</code> 格式，或者仅仅指定容器的端口（宿主将会随机选择端口）都可以。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>ports:
 - &quot;3000&quot;
 - &quot;8000:8000&quot;
 - &quot;49100:22&quot;
 - &quot;127.0.0.1:8001:8001&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><em>注意：当使用</em> <em><code>HOST:CONTAINER</code></em> <em>格式来映射端口时，如果你使用的容器端口小于 60 并且没放到引号里，可能会得到错误结果，因为</em> <em><code>YAML</code></em> <em>会自动解析</em> <em><code>xx:yy</code></em> <em>这种数字格式为 60 进制。为避免出现这种问题，建议数字串都采用引号包括起来的字符串格式。</em></p> <h3 id="secrets"><a href="#secrets" class="header-anchor">#</a> <strong><code>secrets</code></strong></h3> <p>存储敏感数据，例如 <code>mysql</code> 服务密码。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: &quot;3.1&quot;
services:

mysql:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
  secrets:
    - db_root_password
    - my_other_secret

secrets:
  my_secret:
    file: ./my_secret.txt
  my_other_secret:
    external: true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="security-opt"><a href="#security-opt" class="header-anchor">#</a> <strong><code>security_opt</code></strong></h3> <p>指定容器模板标签（label）机制的默认属性（用户、角色、类型、级别等）。例如配置标签的用户名和角色名。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>security_opt:
    - label:user:USER
    - label:role:ROLE
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="stop-signal"><a href="#stop-signal" class="header-anchor">#</a> <strong><code>stop_signal</code></strong></h3> <p>设置另一个信号来停止容器。在默认情况下使用的是 SIGTERM 停止容器。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>stop_signal: SIGUSR1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="sysctls"><a href="#sysctls" class="header-anchor">#</a> <strong><code>sysctls</code></strong></h3> <p>配置容器内核参数。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>sysctls:
  net.core.somaxconn: 1024
  net.ipv4.tcp_syncookies: 0

sysctls:
  - net.core.somaxconn=1024
  - net.ipv4.tcp_syncookies=0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="ulimits"><a href="#ulimits" class="header-anchor">#</a> <strong><code>ulimits</code></strong></h3> <p>指定容器的 ulimits 限制值。</p> <p>例如，指定最大进程数为 65535，指定文件句柄数为 20000（软限制，应用可以随时修改，不能超过硬限制） 和 40000（系统硬限制，只能 root 用户提高）。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>  ulimits:
    nproc: 65535
    nofile:
      soft: 20000
      hard: 40000
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="volumes"><a href="#volumes" class="header-anchor">#</a> <strong><code>volumes</code></strong></h3> <p>数据卷所挂载路径设置。可以设置为宿主机路径(<code>HOST:CONTAINER</code>)或者数据卷名称(<code>VOLUME:CONTAINER</code>)，并且可以设置访问模式 （<code>HOST:CONTAINER:ro</code>）。</p> <p>该指令中路径支持相对路径。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>volumes:
 - /var/lib/mysql
 - cache/:/tmp/cache
 - ~/configs:/etc/configs/:ro
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果路径为数据卷名称，必须在文件中配置数据卷。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: &quot;3&quot;

services:
  my_src:
    image: mysql:8.0
    volumes:
      - mysql_data:/var/lib/mysql

volumes:
  mysql_data:
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h2 id="其它指令"><a href="#其它指令" class="header-anchor">#</a> <strong>其它指令</strong></h2> <p>此外，还有包括 <code>domainname, entrypoint, hostname, ipc, mac_address, privileged, read_only, shm_size, restart, stdin_open, tty, user, working_dir</code> 等指令，基本跟 <code>docker run</code> 中对应参数的功能一致。</p> <p>指定服务容器启动后执行的入口文件。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>entrypoint: /code/entrypoint.sh
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>指定容器中运行应用的用户名。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>user: nginx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>指定容器中工作目录。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>working_dir: /code
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>指定容器中搜索域名、主机名、mac 地址等。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>domainname: your_website.com
hostname: test
mac_address: 08-00-27-00-0C-0A
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>允许容器中运行一些特权命令。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>privileged: true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>指定容器退出后的重启策略为始终重启。该命令对保持服务始终运行十分有效，在生产环境中推荐配置为 <code>always</code> 或者 <code>unless-stopped</code>。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>restart: always
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>以只读模式挂载容器的 root 文件系统，意味着不能对容器内容进行修改。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>read_only: true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>打开标准输入，可以接受外部输入。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>stdin_open: true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>模拟一个伪终端。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>tty: true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="读取变量"><a href="#读取变量" class="header-anchor">#</a> <strong>读取变量</strong></h2> <p>Compose 模板文件支持动态读取主机的系统环境变量和当前目录下的 <code>.env</code> 文件中的变量。</p> <p>例如，下面的 Compose 文件将从运行它的环境中读取变量 <code>${MONGO_VERSION}</code> 的值，并写入执行的指令中。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>version: &quot;3&quot;
services:

db:
  image: &quot;mongo:${MONGO_VERSION}&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>如果执行 <code>MONGO_VERSION=3.2 docker-compose up</code> 则会启动一个 <code>mongo:3.2</code> 镜像的容器；如果执行 <code>MONGO_VERSION=2.8 docker-compose up</code> 则会启动一个 <code>mongo:2.8</code> 镜像的容器。</p> <p>若当前目录存在 <code>.env</code> 文件，执行 <code>docker-compose</code> 命令时将从该文件中读取变量。</p> <p>在当前目录新建 <code>.env</code> 文件并写入以下内容。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code># 支持 # 号注释
MONGO_VERSION=3.6
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>执行 <code>docker-compose up</code> 则会启动一个 <code>mongo:3.6</code> 镜像的容器。</p> <h2 id="参考资料"><a href="#参考资料" class="header-anchor">#</a> <strong>参考资料</strong></h2> <ul><li><a href="https://docs.docker.com/compose/compose-file/" target="_blank" rel="noopener noreferrer">官方文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/docker/awesome-compose" target="_blank" rel="noopener noreferrer">awesome-compose<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">1/28/2022, 8:09:41 AM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-2" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#compose-模板文件" class="sidebar-link reco-side-compose-模板文件" data-v-70334359>Compose 模板文件</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#build" class="sidebar-link reco-side-build" data-v-70334359>`build`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#cap-add-cap-drop" class="sidebar-link reco-side-cap-add-cap-drop" data-v-70334359>`cap_add, cap_drop`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#command" class="sidebar-link reco-side-command" data-v-70334359>`command`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#configs" class="sidebar-link reco-side-configs" data-v-70334359>`configs`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#cgroup-parent" class="sidebar-link reco-side-cgroup-parent" data-v-70334359>`cgroup_parent`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#container-name" class="sidebar-link reco-side-container-name" data-v-70334359>`container_name`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#deploy" class="sidebar-link reco-side-deploy" data-v-70334359>`deploy`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#depends-on" class="sidebar-link reco-side-depends-on" data-v-70334359>`depends_on`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#dns" class="sidebar-link reco-side-dns" data-v-70334359>`dns`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#tmpfs" class="sidebar-link reco-side-tmpfs" data-v-70334359>`tmpfs`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#env-file" class="sidebar-link reco-side-env-file" data-v-70334359>`env_file`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#environment" class="sidebar-link reco-side-environment" data-v-70334359>`environment`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#expose" class="sidebar-link reco-side-expose" data-v-70334359>`expose`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#external-links" class="sidebar-link reco-side-external-links" data-v-70334359>`external_links`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#extra-hosts" class="sidebar-link reco-side-extra-hosts" data-v-70334359>`extra_hosts`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#healthcheck" class="sidebar-link reco-side-healthcheck" data-v-70334359>`healthcheck`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#image" class="sidebar-link reco-side-image" data-v-70334359>`image`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#labels" class="sidebar-link reco-side-labels" data-v-70334359>`labels`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#links" class="sidebar-link reco-side-links" data-v-70334359>`links`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#logging" class="sidebar-link reco-side-logging" data-v-70334359>`logging`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#network-mode" class="sidebar-link reco-side-network-mode" data-v-70334359>`network_mode`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#networks" class="sidebar-link reco-side-networks" data-v-70334359>`networks`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#pid" class="sidebar-link reco-side-pid" data-v-70334359>`pid`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#ports" class="sidebar-link reco-side-ports" data-v-70334359>`ports`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#secrets" class="sidebar-link reco-side-secrets" data-v-70334359>`secrets`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#security-opt" class="sidebar-link reco-side-security-opt" data-v-70334359>`security_opt`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#stop-signal" class="sidebar-link reco-side-stop-signal" data-v-70334359>`stop_signal`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#sysctls" class="sidebar-link reco-side-sysctls" data-v-70334359>`sysctls`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#ulimits" class="sidebar-link reco-side-ulimits" data-v-70334359>`ulimits`</a></li><li class="level-3" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#volumes" class="sidebar-link reco-side-volumes" data-v-70334359>`volumes`</a></li><li class="level-2" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#其它指令" class="sidebar-link reco-side-其它指令" data-v-70334359>其它指令</a></li><li class="level-2" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#读取变量" class="sidebar-link reco-side-读取变量" data-v-70334359>读取变量</a></li><li class="level-2" data-v-70334359><a href="/blogs/Docker/docker-compose-template.html#参考资料" class="sidebar-link reco-side-参考资料" data-v-70334359>参考资料</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="reco-bgm-panel" data-v-b1d3339e><audio id="bgm" src="/music/1.m4a" data-v-b1d3339e></audio> <div class="reco-float-box" style="bottom:200px;z-index:999999;display:none;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><img src="https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg" data-v-b1d3339e></div> <div class="reco-bgm-box" style="left:10px;bottom:10px;z-index:999999;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="reco-bgm-cover" style="background-image:url(https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg);" data-v-b1d3339e><div class="mini-operation" style="display:none;" data-v-b1d3339e><i class="reco-bgm reco-bgm-pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play" style="display:none;" data-v-b1d3339e></i></div> <div class="falut-message" style="display:none;" data-v-b1d3339e>
          播放失败
        </div></div> <div class="reco-bgm-info" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-music music" data-v-b1d3339e></i>苍穹</div> <div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-artist" data-v-b1d3339e></i>韩磊</div> <div class="reco-bgm-progress" data-v-b1d3339e><div class="progress-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div> <div class="reco-bgm-operation" data-v-b1d3339e><i class="reco-bgm reco-bgm-last last" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-pause pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play play" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-next next" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-volume1 volume" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-mute mute" style="display:none;" data-v-b1d3339e></i> <div class="volume-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div></div> <div class="reco-bgm-left-box" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><i class="reco-bgm reco-bgm-left" data-v-b1d3339e></i></div></div></div><!----></div></div>
    <script src="/assets/js/app.b9ccfc55.js" defer></script><script src="/assets/js/4.8390fbcb.js" defer></script><script src="/assets/js/1.3334f995.js" defer></script><script src="/assets/js/34.ba79122c.js" defer></script>
  </body>
</html>
